package com.carrental.search.controller;

import com.carrental.common.api.PageResult;
import com.carrental.common.result.Result;
import com.carrental.search.model.document.VehicleDocument;
import com.carrental.search.service.VehicleSearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 车辆搜索控制器
 * 提供车辆搜索相关的API接口
 */
@Tag(name = "车辆搜索接口", description = "提供车辆搜索相关的API接口")
@RestController
@RequestMapping("/vehicle")
public class VehicleSearchController {

    @Resource
    private VehicleSearchService vehicleSearchService;

    /**
     * 保存车辆索引文档
     *
     * @param vehicleDocument 车辆文档
     * @return 保存结果
     */
    @Operation(summary = "保存车辆索引文档", description = "保存车辆索引文档")
    @PostMapping
    public Result<VehicleDocument> saveVehicle(@RequestBody VehicleDocument vehicleDocument) {
        return Result.success(vehicleSearchService.saveVehicle(vehicleDocument));
    }

    /**
     * 批量保存车辆索引文档
     *
     * @param vehicleDocuments 车辆文档列表
     * @return 保存结果
     */
    @Operation(summary = "批量保存车辆索引文档", description = "批量保存车辆索引文档")
    @PostMapping("/batch")
    public Result<Iterable<VehicleDocument>> saveVehicles(@RequestBody List<VehicleDocument> vehicleDocuments) {
        return Result.success(vehicleSearchService.saveVehicles(vehicleDocuments));
    }

    /**
     * 根据ID获取车辆文档
     *
     * @param id 车辆ID
     * @return 车辆文档
     */
    @Operation(summary = "根据ID获取车辆文档", description = "根据ID获取车辆文档")
    @GetMapping("/{id}")
    public Result<VehicleDocument> getVehicleById(
            @Parameter(description = "车辆ID", required = true) @PathVariable Long id) {
        return Result.success(vehicleSearchService.getVehicleById(id));
    }

    /**
     * 根据ID删除车辆文档
     *
     * @param id 车辆ID
     * @return 删除结果
     */
    @Operation(summary = "根据ID删除车辆文档", description = "根据ID删除车辆文档")
    @DeleteMapping("/{id}")
    public Result<Void> deleteVehicleById(@Parameter(description = "车辆ID", required = true) @PathVariable Long id) {
        vehicleSearchService.deleteVehicleById(id);
        return Result.success();
    }

    /**
     * 搜索车辆
     *
     * @param keyword  关键词
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "搜索车辆", description = "根据关键词搜索车辆")
    @GetMapping("/search")
    public Result<PageResult<VehicleDocument>> searchVehicles(
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.searchVehicles(keyword, pageNum, pageSize));
    }

    /**
     * 高级搜索车辆
     *
     * @param params   搜索参数
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "高级搜索车辆", description = "根据多个条件搜索车辆")
    @PostMapping("/search/advanced")
    public Result<PageResult<VehicleDocument>> advancedSearchVehicles(
            @Parameter(description = "搜索参数", required = true) @RequestBody Map<String, Object> params,
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.advancedSearchVehicles(params, pageNum, pageSize));
    }

    /**
     * 根据品牌搜索车辆
     *
     * @param brand    品牌
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "根据品牌搜索车辆", description = "根据品牌搜索车辆")
    @GetMapping("/search/brand/{brand}")
    public Result<PageResult<VehicleDocument>> searchVehiclesByBrand(
            @Parameter(description = "品牌", required = true) @PathVariable String brand,
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.searchVehiclesByBrand(brand, pageNum, pageSize));
    }

    /**
     * 根据类型搜索车辆
     *
     * @param type     类型
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "根据类型搜索车辆", description = "根据类型搜索车辆")
    @GetMapping("/search/type/{type}")
    public Result<PageResult<VehicleDocument>> searchVehiclesByType(
            @Parameter(description = "类型", required = true) @PathVariable String type,
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.searchVehiclesByType(type, pageNum, pageSize));
    }

    /**
     * 根据价格范围搜索车辆
     *
     * @param minPrice 最小价格
     * @param maxPrice 最大价格
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "根据价格范围搜索车辆", description = "根据价格范围搜索车辆")
    @GetMapping("/search/price")
    public Result<PageResult<VehicleDocument>> searchVehiclesByPriceRange(
            @Parameter(description = "最小价格", required = true) @RequestParam BigDecimal minPrice,
            @Parameter(description = "最大价格", required = true) @RequestParam BigDecimal maxPrice,
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.searchVehiclesByPriceRange(minPrice, maxPrice, pageNum, pageSize));
    }

    /**
     * 根据位置搜索附近的车辆
     *
     * @param latitude  纬度
     * @param longitude 经度
     * @param distance  距离（公里）
     * @param pageNum   页码
     * @param pageSize  每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "根据位置搜索附近的车辆", description = "根据位置搜索附近的车辆")
    @GetMapping("/search/location")
    public Result<PageResult<VehicleDocument>> searchVehiclesByLocation(
            @Parameter(description = "纬度", required = true) @RequestParam double latitude,
            @Parameter(description = "经度", required = true) @RequestParam double longitude,
            @Parameter(description = "距离（公里）", required = true) @RequestParam(defaultValue = "10") double distance,
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(
                vehicleSearchService.searchVehiclesByLocation(latitude, longitude, distance, pageNum, pageSize));
    }

    /**
     * 获取推荐车辆
     *
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "获取推荐车辆", description = "获取推荐车辆")
    @GetMapping("/recommended")
    public Result<PageResult<VehicleDocument>> getRecommendedVehicles(
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.getRecommendedVehicles(pageNum, pageSize));
    }

    /**
     * 获取热门车辆
     *
     * @param limit 限制数量
     * @return 热门车辆列表
     */
    @Operation(summary = "获取热门车辆", description = "获取热门车辆")
    @GetMapping("/hot")
    public Result<PageResult<VehicleDocument>> getHotVehicles(
            @Parameter(description = "限制数量", required = false) @RequestParam(defaultValue = "8") int limit) {
        return Result.success(vehicleSearchService.getHotVehicles(1, limit));
    }

    /**
     * 获取高评分车辆
     *
     * @param minRating 最小评分
     * @param pageNum   页码
     * @param pageSize  每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "获取高评分车辆", description = "获取高评分车辆")
    @GetMapping("/high-rated")
    public Result<PageResult<VehicleDocument>> getHighRatedVehicles(
            @Parameter(description = "最小评分", required = true) @RequestParam(defaultValue = "4.0") Float minRating,
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.getHighRatedVehicles(minRating, pageNum, pageSize));
    }

    /**
     * 获取最新上架的车辆
     *
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 分页车辆文档
     */
    @Operation(summary = "获取最新上架的车辆", description = "获取最新上架的车辆")
    @GetMapping("/newest")
    public Result<PageResult<VehicleDocument>> getNewestVehicles(
            @Parameter(description = "页码", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小", required = true) @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(vehicleSearchService.getNewestVehicles(pageNum, pageSize));
    }

    /**
     * 更新车辆状态
     *
     * @param id     车辆ID
     * @param status 状态
     * @return 更新后的车辆文档
     */
    @Operation(summary = "更新车辆状态", description = "更新车辆状态")
    @PutMapping("/{id}/status/{status}")
    public Result<VehicleDocument> updateVehicleStatus(
            @Parameter(description = "车辆ID", required = true) @PathVariable Long id,
            @Parameter(description = "状态", required = true) @PathVariable Integer status) {
        return Result.success(vehicleSearchService.updateVehicleStatus(id, status));
    }

    /**
     * 记录车辆浏览次数
     *
     * @param id 车辆ID
     * @return 操作结果
     */
    @Operation(summary = "记录车辆浏览次数", description = "增加车辆浏览计数，用于计算热门指数")
    @PostMapping("/{id}/view")
    public Result<Boolean> recordVehicleView(@PathVariable("id") Long id) {
        return Result.success(vehicleSearchService.incrementViewCount(id));
    }
}